<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('课程时间设定')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <form class="form-horizontal m">
                <div class="form-group bg-white">
                    <div class="col-sm-12">
                        <label class="font-bold" th:text="${configVo.name + '('+ configVo.gradeName}+')--课程时间设置'"></label>
                    </div>
                </div>
            </form>

            <div class="col-sm-12 select-table table-striped table-bordered">
                <table id="bootstrap-table-time"></table>
            </div>
            <div id="modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="largerModal">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">时间设置</div>
                        <div class="modal-body p-sm">
                            <form class="form-horizontal m" id="add-course-time-info">
                                <input name="mainId" type="text" id="mainId" th:value="${configVo.id}" class="hidden">
                                <input id="id" name="id" class="hidden" type="text"/>
                                <input id="clazzId" name="clazzId" class="hidden" type="text"/>
                                <input id="rid" name="rid" class="hidden" type="text"/>

                                <div class="form-group">
                                    <label class="col-sm-3 control-label is-required">节次：</label>
                                    <div class="col-sm-8">
                                        <input id="section" name="section" type="text" class="form-control" readonly>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label is-required">开始时间：</label>
                                    <div class="col-sm-8 date timePicker">
                                        <input id="btime" name="btime" type="text" class="form-control" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label is-required">结束时间：</label>
                                    <div class="col-sm-8 date timePicker">
                                        <input id="etime" name="etime" type="text" class="form-control" required>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                            <button type="button" class="btn btn-primary" onclick="setCourseOK()">确定</button>
                        </div>
                    </div>
                </div>
            </div>

        </div>

    </div>
    <th:block th:include="include :: footer" />
     <th:block th:include="include :: datetimepicker-js" />
     <script th:src="@{/js/common/date_utils.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "school/courseConfig";
        var configMain = [[${configVo}]]
        var timeList = [[${timeList}]]
        var options = {
            columns: [
                {
                    field: 'id',
                    title: 'Id',
                    visible:false
                },
                {
                    field: 'section',
                    title: '节次',
                    width:80
                },
                {
                    field: 'mainId',
                    title: 'mainId',
                    visible:false
                },
                {
                    field: 'btime',
                    title: '开始时间',
                    // formatter: dateFormatterHHMM,
                    width:120
                },
                {
                    field: 'etime',
                    title: '结束时间',
                    // formatter: dateFormatterHHMM,
                    width:120
                },
                {
                    title: '操作',
                    width:160,
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs " href="javascript:void(0)" onclick="editRow(\'' + index + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        return actions.join('');
                    }
                }
            ],
            data:timeList
        }

        $("#add-course-time-info").validate({
            focusCleanup: true
        });
        $(function () {
            $("#bootstrap-table-time").bootstrapTable(options);
        });

        function editRow(row) {
            console.log(row);
            let _row = timeList[row];
            $('#section').val(_row.section);
            $('#rid').val(row);
            $('#btime').val(_row.btime);
            $('#etime').val(_row.etime);
            $('#modal').modal("show");
        }

        function setCourseOK() {
            //点击确定按钮
            let _r = $('#rid').val();
            let _index = parseInt(_r);
            let rowData = timeList[_index];
            console.log(rowData,timeList,_index);
            rowData.bTime = $('#btime').val();
            rowData.btime = $('#btime').val();
            rowData.eTime = $('#etime').val();
            rowData.etime = $('#etime').val();
            rowData.mainId = $('#mainId').val();
            if(rowData.bTime&&rowData.eTime){
                $.ajax({
                    type: "post",
                    url:prefix+'/saveCourseTime',
                    async:false,
                    dataType: "json",
                    data:rowData,
                    success:function (res) {
                        console.log(res);//保存成功
                        if(res.code==0){
                            $("#bootstrap-table-time").bootstrapTable('updateRow', {
                                index: _r,
                                replace:true,
                                row:rowData
                            });
                            $('#modal').modal("hide");
                        }else{
                            $.modal.alertWarning(res.msg);
                        }
                    }

                });
            }else{
                $.modal.alertWarning("请输入开始时间和结束时间");
            }

        }



        $("input[name='btime']").datetimepicker({
            format: 'hh:ii',
            startView: 0,
            minView: 0,
            viewSelect: 0,
            pickDate: false,
            minuteStep: 5,
            autoclose: true
        });
        $("input[name='etime']").datetimepicker({
            format: 'hh:ii',
            startView: 0,
            minView: 0,
            viewSelect: 0,
            pickDate: false,

            minuteStep: 5,
            autoclose: true
        });


    </script>
</body>
</html>